const userModel = require('../model/userModel');
//新增
module.exports.insert = async function(user){
    return await userModel.create(user);
}

//查询所有
module.exports.find = async function (page, searchParams) {
    // return await studentModel.find(searchParams);
    //分页
    let { current, pageSize } = page;
    console.log(current, pageSize);
    //根据查询条件获取总数
    let total = await userModel.countDocuments(searchParams);
    //根据查询条件获取具体数量的数据
    let data = await userModel.find(searchParams)
        // .populate({path:"Classes"})//集合名称
        .limit(pageSize)
        .skip((current - 1) * pageSize);
    return {
        current, pageSize, total, rows: data,
    }
}

//id查询
module.exports.findById = async function (id) {
    return await userModel.findById(id);
}

//删除
module.exports.delete = async function (id) {
    return await userModel.deleteOne({ _id: id });//删除一个，数据库的id有_，所有这里要跟数据那里的写法要匹配
}

//修改
module.exports.update = async function (user) {
    return await userModel.updateOne({ _id: user._id }, user);//按照id来修改。学生的id
}

//匹配账号密码的那个
module.exports.findByUsernameAndPassword = async function(user){
    return await userModel.findOne(user);
}
//查询注册名称是否重复
module.exports.findByUsername = async function(username){
    return await userModel.find({username});
}